package com.zhkjnet.zhyy.controller;

import com.zhkjnet.zhyy.bean.RequiredPermission;
import com.zhkjnet.zhyy.bean.Roles;
import com.zhkjnet.zhyy.entity.Asset;
import com.zhkjnet.zhyy.service.AssetService;
import com.zhkjnet.zhyy.service.OilCardService;
import com.zhkjnet.zhyy.service.OilRechargeService;
import com.zhkjnet.zhyy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

@Controller
public class HomeController {

    @Autowired
    private HttpSession session;

    @Autowired
    private OilCardService oilCardService;

    @Autowired
    private AssetService assetService;

    @Autowired
    private OilRechargeService oilRechargeService;

    @Autowired
    private UserService userService;

    private Long getMinMoney(Asset asset) {
        Long money = asset.getMinMoney() - asset.getMoney();
        return money > 0 ? money : 0;
    }

    /**
     * 首页，油卡列表
     * @return
     */
    @RequiredPermission(Roles.User)
    @GetMapping("/")
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView("home/oilCards.html");
        Long userId = (long) session.getAttribute("id");
        Asset asset = assetService.findByUserId(userId);
        mv.addObject("oilCards", oilCardService.findAllByUserId(userId));
        mv.addObject("asset", asset);
        mv.addObject("show", asset.getMoney() <= asset.getMinMoney() * 0.6);
        mv.addObject("money", getMinMoney(asset));
        return mv;
    }

    /**
     * 添加油卡
     * @return
     */
    @RequiredPermission(Roles.User)
    @GetMapping("/addOilCard")
    public ModelAndView addOilCard() {
        ModelAndView mv = new ModelAndView("home/addOilCard.html");
        return mv;
    }

    /**
     * 油卡详情
     * @return
     */
    @RequiredPermission(Roles.User)
    @GetMapping("/oilCard/{id}")
    public ModelAndView oilCard(@PathVariable("id") Long id) {
        ModelAndView mv = new ModelAndView("home/oilCard.html");
        mv.addObject("oilCard", oilCardService.findById(id));
        return mv;
    }

    /**
     * 充值
     * @return
     */
    @RequiredPermission(Roles.User)
    @GetMapping("/recharge")
    public ModelAndView recharge() {
        Long userId = (Long) session.getAttribute("id");
        ModelAndView mv = new ModelAndView("home/recharge.html");
        mv.addObject("min", assetService.findMinRechargeMoneyByUserId(userId));
        return mv;
    }

    /**
     * 个人中心
     * @return
     */
    @RequiredPermission(Roles.User)
    @GetMapping("/me")
    public ModelAndView me() {
        ModelAndView mv = new ModelAndView("home/me.html");
        Long userId = (Long) session.getAttribute("id");
        Asset asset = assetService.findByUserId(userId);
        mv.addObject("user", userService.findById(userId));
        mv.addObject("asset", asset);
        mv.addObject("money", getMinMoney(asset));
        return mv;
    }

    /**
     * 登录
     * @return
     */
    @GetMapping("/login")
    public String login() {
        return "home/login.html";
    }

    /**
     * 油卡充值记录
     * @param id
     * @return
     */
    @RequiredPermission(Roles.User)
    @GetMapping("/oilRecharge")
    public ModelAndView oilRecharge(Long id) {
        ModelAndView mv = new ModelAndView("home/oilRecharge.html");
        mv.addObject("recharges", oilRechargeService.findAllByCardId(id));
        return mv;
    }

    @RequiredPermission(Roles.User)
    @GetMapping("/oilRecharges")
    public String oilRecharges() {
        return "home/oilRecharges.html";
    }

    /**
     * 退出登陆
     * @return
     */
    @RequiredPermission(Roles.User)
    @GetMapping("/logout")
    public String logout() {
        session.removeAttribute("id");
        session.removeAttribute("role");
        return "redirect:login";
    }

    @RequiredPermission(Roles.User)
    @GetMapping("/moneyRecords")
    public String moneyRecords() {
        return "home/moneyRecords.html";
    }

    /**
     * 修改密码
     * @return
     */
    @RequiredPermission(Roles.User)
    @GetMapping("/password")
    public String password() {
        return "home/password.html";
    }

}
